package 中断;

public class 自定义标志位 {
    //先设计一个标志位，表示是否被中断
    public static volatile boolean isStop = false;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //判断条件加上标志位
                    for (int i = 0; i < 10 && !isStop; i++) {
                        System.out.println(i);
                        //时间长了就没法中断
                        Thread.sleep(10000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        //中断t线程
        Thread.sleep(3000);
        isStop=true;
    }
}
